package com.online.robot.wechat.helper;

import com.google.common.collect.ImmutableMap;
import com.online.robot.wechat.config.WechatMpProperties;
import com.join.robot.services.wechat.handler.*;
import com.online.robot.wechat.handler.*;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @author happy on 2018-11-23.
 */
@Component
public class WechatMpHelper {

    @Autowired
    private LogHandler logHandler;
    @Autowired
    private MsgHandler msgHandler;
    @Autowired
    private SubscribeHandler subscribeHandler;
    @Autowired
    private UnSubscribeHandler unSubscribeHandler;
    @Autowired
    private LinkElmHandler linkElmHandler;
    @Autowired
    private WechatMpProperties properties;

    private Map<String, WxMpMessageRouter> routerMap;
    private Map<String, WxMpService> serviceMap;

    public WxMpMessageRouter getRouter(String appId) {
        return routerMap.get(appId);
    }

    public WxMpService getService(String appId) {
        return serviceMap.get(appId);
    }

    @PostConstruct
    public void init() {
        ImmutableMap.Builder<String, WxMpMessageRouter> routerMapBuilder = ImmutableMap.builder();
        ImmutableMap.Builder<String, WxMpService> serviceMapBuilder = ImmutableMap.builder();

        properties.getConfigs().forEach(a -> {
            WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
            configStorage.setAppId(a.getAppId());
            configStorage.setSecret(a.getSecret());
            configStorage.setToken(a.getToken());
            configStorage.setAesKey(a.getAesKey());

            WxMpService wxMpService = new me.chanjar.weixin.mp.api.impl.WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(configStorage);

            routerMapBuilder.put(a.getAppId(), router(wxMpService));
            serviceMapBuilder.put(a.getAppId(), wxMpService);
        });

        routerMap = routerMapBuilder.build();
        serviceMap = serviceMapBuilder.build();
    }


    private WxMpMessageRouter router(WxMpService wxMpService) {

        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);

        //记录所有事件的日志 （异步执行）
        //newRouter.rule().handler(this.logHandler).next();

        // 关注事件
        newRouter.rule()
                .async(false)
                .msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.SUBSCRIBE)
                .handler(subscribeHandler).end();

        // 取消关注
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.UNSUBSCRIBE)
                .handler(unSubscribeHandler).end();
        //链接
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.LINK).handler(linkElmHandler).end();

        // 默认
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.TEXT).handler(msgHandler).end();

        return newRouter;
    }
}
